package CollectionsDay01;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class GenericDemo {
    public static void main(String[] args) {
        Collection<String> c = new ArrayList<>();
        c.add("one");
        c.add("two");
        c.add("three");
        c.add("four");
        c.add("five");
        //c.add(1,2,3);//编译错误,参数类型违背了集合c所制定的泛型E的实际类型String

        //迭代器所制定的泛型应当与其遍历的集合的泛型一致
        Iterator<String> it = c.iterator();
        while(it.hasNext()){
            String str = it.next();//制定泛型后,获取元素无需再手动强转
            System.out.println(str);
        }
        for (String str : c){
            System.out.println(str);
        }
    }
}
